package com.bwie.sixth.house.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bwie.sixth.house.config.PageUtil;
import com.bwie.sixth.house.vo.NewEsVo;
import com.bwie.sixth.house.vo.OldEsQuaryVo;
import com.bwie.sixth.house.vo.OldEsVo;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.bwie.sixth.house.dao.OldHouseDao;
import com.bwie.sixth.house.entity.OldHouseEntity;
import com.bwie.sixth.house.service.OldHouseService;


@Service("oldHouseService")
public class OldHouseServiceImpl extends ServiceImpl<OldHouseDao, OldHouseEntity> implements OldHouseService {

    @Autowired
    private RestHighLevelClient client;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OldHouseEntity> page = this.page(
                new Query<OldHouseEntity>().getPage(params),
                new QueryWrapper<OldHouseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtil oldEsList(OldEsQuaryVo oldEsQuaryVo, int size, int page) throws IOException {
        SearchRequest searchRequest = new SearchRequest("old");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (oldEsQuaryVo!=null){
            if (oldEsQuaryVo.getMapId()!=null&&oldEsQuaryVo.getMapId()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("mapId",oldEsQuaryVo.getMapId()));
            }
            if (oldEsQuaryVo.getOldFeat()!=null&&oldEsQuaryVo.getOldFeat()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("oldFeat",oldEsQuaryVo.getOldFeat()));
            }
            if (oldEsQuaryVo.getOldFinish()!=null&&oldEsQuaryVo.getOldFinish()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("oldFinish",oldEsQuaryVo.getOldFinish()));
            }
            if (oldEsQuaryVo.getOldRoom()!=null&&oldEsQuaryVo.getOldRoom()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("oldRoom",oldEsQuaryVo.getOldRoom()));
            }
            if (oldEsQuaryVo.getOldType()!=null&&oldEsQuaryVo.getOldType()!=0){
                boolQueryBuilder.must(QueryBuilders.matchQuery("oldType",oldEsQuaryVo.getOldType()));
            }
            if (oldEsQuaryVo.getOldBigPrice()!=null&&oldEsQuaryVo.getOldSmallPrice()!=null&&oldEsQuaryVo.getOldBigPrice()!=0&&oldEsQuaryVo.getOldSmallPrice()!=0){
                boolQueryBuilder.filter(QueryBuilders.rangeQuery("oldPrice")
                        .from(oldEsQuaryVo.getOldSmallPrice()).to(oldEsQuaryVo.getOldBigPrice()));
            }
            if (oldEsQuaryVo.getOldBigArea()!=null&&oldEsQuaryVo.getOldSmallArea()!=null&&oldEsQuaryVo.getOldBigArea()!=0&&oldEsQuaryVo.getOldSmallArea()!=0){
                boolQueryBuilder.filter(QueryBuilders.rangeQuery("oldArea")
                        .from(oldEsQuaryVo.getOldSmallArea()).to(oldEsQuaryVo.getOldBigArea()));
            }
            searchSourceBuilder.query(boolQueryBuilder);
        }
        else{
            searchSourceBuilder.query(boolQueryBuilder);
        }
        searchSourceBuilder.size(size);
        searchSourceBuilder.from((page-1)*size);

        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        SearchHits hits = searchResponse.getHits();
        TotalHits totalHits = hits.getTotalHits();
// the total number of hits, must be interpreted in the context of totalHits.relation
        long numHits = totalHits.value;
// whether the number of hits is accurate (EQUAL_TO) or a lower bound of the total (GREATER_THAN_OR_EQUAL_TO)
        TotalHits.Relation relation = totalHits.relation;
        float maxScore = hits.getMaxScore();
        SearchHit[] searchHits = hits.getHits();

        ArrayList<OldEsVo> listVos = new ArrayList<>();
        for (SearchHit hit : searchHits) {
            // do something with the SearchHit
            String index = hit.getIndex();
            String id = hit.getId();
            float score = hit.getScore();
            String sourceAsString = hit.getSourceAsString();
            OldEsVo oldEsVo = JSON.parseObject(sourceAsString, new TypeReference<OldEsVo>() {
            });
            oldEsVo.setOldId(Integer.valueOf(id));
            listVos.add(oldEsVo);

        }
        return new PageUtil<OldEsVo>(page,numHits,listVos,size);
    }

}